﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Xml;
using Sherwood.Web;

namespace Sherwood.Content.SearchServices
{
    public class ForeignIdInjector<TRecord> : ISolrXmlInjector<TRecord> where TRecord : IRecord
    {
        public void Prepare(QueryParameterCollection queryString)
        {
            
        }

        public object Prefetch(IEnumerable<Guid> recordIds, Func<IEnumerable<TRecord>> getRecords, out bool needsRecordsOnInjection)
        {
            needsRecordsOnInjection = true;
            return null;
        }

        public void Inject(NameValueCollection solrValues, Guid recordId, TRecord record, object prefetchValues, XmlWriter writer)
        {
            if (record != null)
            {
                writer.WriteStartElement("str");
                writer.WriteAttributeString("name", "reference_id");
                writer.WriteCData(record.ReferenceId);
                writer.WriteEndElement();
            }
        }
    }
}